package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.*;
import com.bestescortbedsystem.entity.*;
import com.bestescortbedsystem.service.YongHuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: LuXi
 * @Date: 2021/2/23 14:35
 * @Description: 用户实现层
 */
@Service
public class YongHuServiceImpl implements YongHuService {
    @Resource
    private SesUserDao sesUserDao;
    @Resource
    private SesUserOrderDao sesUserOrderDao;
    @Resource
    private SesBedMessageDao sesBedMessageDao;
    @Resource
    private SesHospitalinfoDao sesHospitalinfoDao;
    @Resource
    private SesHospitalinfoDepartmentDao sesHospitalinfoDepartmentDao;
    @Resource
    private SesUserSharingDao sesUserSharingDao;
    @Resource
    private SesUserReportDao sesUserReportDao;
    @Resource
    private SesUserFeedbackDao sesUserFeedbackDao;

    /**
     * 获取所有股东信息 和 投资信息 （包括查询 分页）
     * @return 返回股东信息集合
     * @param guGongName
     * @param minMoney
     * @param maxMoney
     * @param pageNumber
     * @param pageSize
     * @param usertype
     */
    @Override
    public Map<String,Object> getAll(String guGongName, String minMoney, String maxMoney, String pageNumber, String pageSize, String usertype) {
        /**
         * 按照条件查询所有股东信息
         */
        SesUserExample sesUserExample=new SesUserExample();
        SesUserExample.Criteria criteria = sesUserExample.createCriteria();
        List<Integer> userTypeId=new ArrayList<>();
        userTypeId.add(4);
        userTypeId.add(10);
        userTypeId.add(11);
        criteria.andUsertypeIn(userTypeId);
        if(!guGongName.equals("")&&guGongName!=null){
            criteria.andUsernameLike("%"+guGongName+"%");
        }
        if(!usertype.equals("")&&usertype!=null){
            criteria.andUsertypeEqualTo(Integer.parseInt(usertype));
        }
        List<SesUser> list = sesUserDao.selectByExample(sesUserExample);
        List<Long> sesUserId =new ArrayList<>();
        for (SesUser li:list) {
            sesUserId.add(li.getUserid());
        }

        /**
         * 按照条件查询所有 股东的资金信息
         */
        List<SesUserSharing> sesUserSharings=new ArrayList<SesUserSharing>();
        SesUserSharingExample sesUserSharingExample=new SesUserSharingExample();
        SesUserSharingExample.Criteria criteria1 = sesUserSharingExample.createCriteria();
        criteria1.andUseridIn(sesUserId);
        if(!minMoney.equals("")&&minMoney!=null&&!maxMoney.equals("")&&maxMoney!=null){
            criteria1.andUsersharingamountBetween(Float.parseFloat(minMoney),Float.parseFloat(maxMoney));
            System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++=====================================+++++++++++++++++++++++++++++++");
        }else {
            if (!minMoney.equals("") && minMoney != null) {
                criteria1.andUsersharingamountGreaterThanOrEqualTo(Float.parseFloat(minMoney));
            }
            if (!maxMoney.equals("") && maxMoney != null) {
               criteria1.andUsersharingamountLessThanOrEqualTo(Float.parseFloat(maxMoney));
            }
        }
        PageHelper.startPage(Integer.parseInt(pageNumber),Integer.parseInt(pageSize));
        List<SesUserSharing> sesUserSharings1 = sesUserSharingDao.selectByExample(sesUserSharingExample);
        PageInfo<SesUserSharing> sesUserSharingPageInfo=new PageInfo<>(sesUserSharings1);
        /**
         *  反查所有股东
         */
        List<SesUserSharing> list1 = sesUserSharingPageInfo.getList();
        List<Long> userId2=new ArrayList<>();
        for (SesUserSharing li:list1) {
            userId2.add(li.getUserid());
        }
        SesUserExample sesUserExample2=new SesUserExample();
        SesUserExample.Criteria criteria2 = sesUserExample2.createCriteria();
        criteria2.andUseridIn(userId2);
        List<SesUser> list2 = sesUserDao.selectByExample(sesUserExample2);
        System.out.println(list2);
        Map<String,Object> map=new HashMap<>();
        map.put("gudong",list2);
        map.put("sharing",list1);
        map.put("page",sesUserSharingPageInfo);
        return map;
    }

    /**
     * 删除股东和
     * @param userId
     * @param usersharingId
     */
    @Override
    public void delGudong(String userId, String usersharingId) {
        sesUserDao.deleteByPrimaryKey(Long.parseLong(userId));
    }

    /**
     * 添加股东和分销商信息
     * @param renYuanXingMing
     * @param renYuanDianHua
     * @param renYuanJueSe
     * @param touZiJinE
     * @param shouYiFneCheng
     * @param guGongEmial
     * @param ruHuoShijian
     * @param RenYuanMiMa
     */
    @Override
    public void TianJiaGuDong(String renYuanXingMing, Long renYuanDianHua, Integer renYuanJueSe, Float touZiJinE, Float shouYiFneCheng, String guGongEmial, String ruHuoShijian,String RenYuanMiMa) {
        /**
         * 向用户表添加股东/分销商信息
         */
        SesUser sesUser=new SesUser();
               sesUser.setUseremail(guGongEmial);
               sesUser.setUsername(renYuanXingMing);
               sesUser.setUserregistrationtime(ruHuoShijian);
               sesUser.setUserphone(renYuanDianHua);
               sesUser.setUsertype(renYuanJueSe);
               sesUser.setUserpassword(RenYuanMiMa);

         sesUserDao.insertSelective(sesUser);
        /**
         * 通过条件查找对应的股东/分销商id
         */
        SesUserExample sesUserExample=new SesUserExample();
         SesUserExample.Criteria criteria = sesUserExample.createCriteria();
        criteria.andUsernameEqualTo(renYuanXingMing);
        criteria.andUserpasswordEqualTo(RenYuanMiMa);
        criteria.andUseremailEqualTo(guGongEmial);
        /**
         * 向用户分成表添加信息
         */
        List<SesUser> list = sesUserDao.selectByExample(sesUserExample);
        Long userid = list.get(0).getUserid();
        SesUserSharing sesUserSharing=new SesUserSharing();
        sesUserSharing.setUserid(userid);
        sesUserSharing.setUsersharing(shouYiFneCheng);
        sesUserSharing.setUsersharingamount(touZiJinE);
        sesUserSharingDao.insertSelective(sesUserSharing);
    }

    /**
     * 股东编辑
     * @param userId
     * @param usersharingId
     * @return
     */
    @Override
    public Map<String, Object> BianJIGuDong(Long userId, Long usersharingId) {
        SesUserSharing sesUserSharing = sesUserSharingDao.selectByPrimaryKey(usersharingId);
        SesUser sesUser = sesUserDao.selectByPrimaryKey(userId);
        Map<String,Object> map=new HashMap<>();
        map.put("sesUser",sesUser);
        map.put("sesUserSharing",sesUserSharing);
        return map;
    }

    @Override
    public void BianJIGuDongBaoCun(String bianJiXingMing, Long bianJiDianHua, Integer bianJiJueSe, Float bianJiJinE, Float bianJiFneCheng, String bianJiEmial, String bianJiMiMa, Long userId, Long usersharingId) {
        //修改股东信息
        SesUser sesUser = new SesUser();
        sesUser.setUseremail(bianJiEmial);
        sesUser.setUsername(bianJiXingMing);
        sesUser.setUserphone(bianJiDianHua);
        sesUser.setUsertype(bianJiJueSe);
        sesUser.setUserpassword(bianJiMiMa);
        sesUser.setUserid(userId);

        sesUserDao.updateByPrimaryKeySelective(sesUser);
        SesUserSharing sesUserSharing = new SesUserSharing();
        sesUserSharing.setUsersharing(bianJiFneCheng);
        sesUserSharing.setUsersharingamount(bianJiJinE);
        sesUserSharing.setUsersharingid(usersharingId);System.out.println(sesUserSharing + "111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
        sesUserSharingDao.updateByPrimaryKeySelective(sesUserSharing);
    }

    /**
     * 得到所有会计信息 （查询 和分页）
     * @param kuaiJiName
     * @param kaishitime
     * @param jieshutime
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> getAllKuaiJi(String kuaiJiName, String kaishitime, String jieshutime, String pageNumber, String pageSize) {
        //增加查询条件
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SesUserExample sesUserExample=new SesUserExample();
        SesUserExample.Criteria criteria = sesUserExample.createCriteria();

        if(!kuaiJiName.equals("")&&kuaiJiName!=null){
            criteria.andUsernameLike("%"+kuaiJiName+"%");
        }
        if(jieshutime!=null&&!jieshutime.equals("")&&kaishitime!=null&&!kaishitime.equals("")){
            try {
                Date date = sdf.parse(jieshutime);
                Date date1 = sdf.parse(kaishitime);
                criteria.andUserregistrationtimeBetween(date1,date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
       if(jieshutime!=null&&!jieshutime.equals("")){
           try {
               Date date = sdf.parse(jieshutime);
               criteria.andUserregistrationtimeLessThanOrEqualTo(date);
           } catch (ParseException e) {
               e.printStackTrace();
           }
       }
       if(kaishitime!=null&&!kaishitime.equals("")) {
           try {
               Date date = sdf.parse(kaishitime);
               criteria.andUserregistrationtimeGreaterThanOrEqualTo(date);

           } catch (ParseException e) {
               e.printStackTrace();
           }
       }
       }
        criteria.andUsertypeIn(Arrays.asList(7));
        //分页
        PageHelper.startPage(Integer.parseInt(pageNumber) ,Integer.parseInt(pageSize));
        List<SesUser> list = sesUserDao.selectByExample(sesUserExample);
        PageInfo<SesUser> pageInfo=new PageInfo<>(list);
        List<SesUser> kuaiji=pageInfo.getList();
        Map<String,Object> map=new HashMap<>();
        System.out.println(pageInfo);
        map.put("kuaiji",kuaiji);
        map.put("page",pageInfo);
        return map;
    }

    /**
     *单个删除会计
     * @param userId
     */
    @Override
    public void delKuaiJi(String userId) {
        sesUserDao.deleteByPrimaryKey(Long.parseLong(userId));
    }

    /**
     * 添加会计
     * @param sesUser
     */
    @Override
    public void TianJiKuaji(SesUser sesUser) {
        sesUser.setUsertype(7);
        sesUserDao.insertSelective(sesUser);
    }
/*
    编辑会计回显数据
 */
    @Override
    public Map<String, Object> BianJiKuaji(Long userId) {
        SesUser sesUser = sesUserDao.selectByPrimaryKey(userId);
        Map<String,Object> map=new HashMap<>();
        map.put("sesUser",sesUser);
        return map;
    }

    /**
     * 编辑股东保存
     * @param sesUser
     */
    @Override
    public void BianJIKuaiJiBaoCun(SesUser sesUser) {
          sesUser.setUsertype(7);
          sesUserDao.updateByPrimaryKeySelective(sesUser);
    }

    /**
     * 查询所有医院对接人和科室对接人（查询和分页）
     * @param renYuanName
     * @param kaishitime
     * @param jieshutime
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> getAllYiYuan(String renYuanName,String userJueSe, String kaishitime, String jieshutime, Integer pageNumber, Integer pageSize) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SesUserExample sesUserExample=new SesUserExample();
        SesUserExample.Criteria criteria = sesUserExample.createCriteria();
        if(!userJueSe.equals("")&&userJueSe!=null){
            criteria.andUsertypeEqualTo(Integer.parseInt(userJueSe));
        }
        if(!renYuanName.equals("")&&renYuanName!=null){
            criteria.andUsernameLike("%"+renYuanName+"%");
        }
        if(jieshutime!=null&&!jieshutime.equals("")&&kaishitime!=null&&!kaishitime.equals("")){
            try {
                Date date = sdf.parse(jieshutime);
                Date date1 = sdf.parse(kaishitime);
                criteria.andUserregistrationtimeBetween(date1,date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            if(jieshutime!=null&&!jieshutime.equals("")){
                try {
                    Date date = sdf.parse(jieshutime);
                    criteria.andUserregistrationtimeLessThanOrEqualTo(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if(kaishitime!=null&&!kaishitime.equals("")) {
                try {
                    Date date = sdf.parse(kaishitime);
                    criteria.andUserregistrationtimeGreaterThanOrEqualTo(date);

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        criteria.andUsertypeIn(Arrays.asList(3,12));
        //分页
        PageHelper.startPage(pageNumber ,pageSize);
        List<SesUser> list = sesUserDao.selectByExample(sesUserExample);
        PageInfo<SesUser> pageInfo=new PageInfo<>(list);
        List<SesUser> yiyuan=pageInfo.getList();
        Map<String,Object> map=new HashMap<>();
        System.out.println(pageInfo);
        map.put("yiyuan",yiyuan);
        map.put("page",pageInfo);
        return map;

    }

    /**
     * 添加医院对接诶人/科室对接人
     * @param sesUser
     */
    @Override
    public void TianJiaYiYuan(SesUser sesUser) {
        sesUserDao.insertSelective(sesUser);
    }

    /**
     * 删除医院对接人/科室对接人
     * @param userId
     */
    @Override
    public void delYIYuan(Long userId) {
        sesUserDao.deleteByPrimaryKey(userId);
    }

    /**
     * 编辑医院对接人/可是对接人（数据回显）
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> BianJiYiYuan(Long userId) {
        SesUser sesUser = sesUserDao.selectByPrimaryKey(userId);
        Map<String,Object> map=new HashMap<>();
        map.put("sesUser",sesUser);
        return map;
    }

    /**
     * 编辑医院对接人/可是对接人(保存修改)
     * @param sesUser
     */
    @Override
    public void BianJIYiYuanBaoCun(SesUser sesUser) {
        sesUserDao.updateByPrimaryKeySelective(sesUser);
    }

    /**
     * 查询所有维修人员信息
     * @param renYuanName
     * @param kaishitime
     * @param jieshutime
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> getAllWeiXiu(String renYuanName, String kaishitime, String jieshutime, Integer pageNumber, Integer pageSize) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SesUserExample sesUserExample=new SesUserExample();
        SesUserExample.Criteria criteria = sesUserExample.createCriteria();

        if(!renYuanName.equals("")&&renYuanName!=null){
            criteria.andUsernameLike("%"+renYuanName+"%");
        }
        if(jieshutime!=null&&!jieshutime.equals("")&&kaishitime!=null&&!kaishitime.equals("")){
            try {
                Date date = sdf.parse(jieshutime);
                Date date1 = sdf.parse(kaishitime);
                criteria.andUserregistrationtimeBetween(date1,date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            if(jieshutime!=null&&!jieshutime.equals("")){
                try {
                    Date date = sdf.parse(jieshutime);
                    criteria.andUserregistrationtimeLessThanOrEqualTo(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if(kaishitime!=null&&!kaishitime.equals("")) {
                try {
                    Date date = sdf.parse(kaishitime);
                    criteria.andUserregistrationtimeGreaterThanOrEqualTo(date);

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        criteria.andUsertypeIn(Arrays.asList(2));

        //分页
        PageHelper.startPage(pageNumber ,pageSize);
        List<SesUser> list = sesUserDao.selectByExample(sesUserExample);
            List<Long> listUserID=new ArrayList<>();
        for (SesUser l:list) {
            listUserID.add(l.getUserid());
        }
        PageInfo<SesUser> pageInfo=new PageInfo<>(list);
        SesHospitalinfoExample sesHospitalinfoExample=new SesHospitalinfoExample();
        SesHospitalinfoExample.Criteria criteria1 = sesHospitalinfoExample.createCriteria();
        criteria1.andUseridIn(listUserID);
        List<SesHospitalinfo> sesHospitalinfos = sesHospitalinfoDao.selectByExample(sesHospitalinfoExample);
        List<SesUser> weixiu=pageInfo.getList();
        Map<String,Object> map=new HashMap<>();
        SesHospitalinfoDepartmentExample sesHospitalinfoDepartmentExample=new SesHospitalinfoDepartmentExample();
        SesHospitalinfoDepartmentExample.Criteria criteria2 = sesHospitalinfoDepartmentExample.createCriteria();
        SesHospitalinfoDepartmentExample.Criteria criteria3 = criteria2.andUseridIn(listUserID);
        List<SesHospitalinfoDepartment> sesHospitalinfoDepartments = sesHospitalinfoDepartmentDao.selectByExample(sesHospitalinfoDepartmentExample);

        map.put("weixiu",weixiu);
        map.put("page",pageInfo);
        map.put("yiyuanname",sesHospitalinfos);
        map.put("keshiname",sesHospitalinfoDepartments);
        return map;
    }

    /**
     * 删除维修人员信息
     * @param userId
     */
    @Override
    public void delWeiXiu(String userId) {
        sesUserDao.deleteByPrimaryKey(Long.parseLong(userId));
    }

    /**
     * 添加维修人员
     * @param TUserName
     * @param TUserphone
     * @param TYiYuan
     * @param TKeShi
     * @param Tuseremail
     * @param Tuserregistrationtime
     * @param Tuserpassword
     */
    @Override
    public void TianJiaWeiXiu(String TUserName, String TUserphone, String TYiYuan, String TKeShi, String Tuseremail, String Tuserregistrationtime, String Tuserpassword) {
        /**
         * 向用户表添加维修人员信息
         */
        SesUser sesUser=new SesUser();
        sesUser.setUsername(TUserName);
        sesUser.setUserphone(Long.parseLong(TUserphone));
        sesUser.setUseremail(Tuseremail);
        sesUser.setUserregistrationtime(Tuserregistrationtime);
        sesUser.setUserpassword(Tuserpassword);
        sesUser.setUsertype(2);
        sesUserDao.insertSelective(sesUser);
        SesUserExample sesUserExample=new SesUserExample();
        SesUserExample.Criteria criteria = sesUserExample.createCriteria();
        criteria.andUsernameEqualTo(TUserName);
        criteria.andUserphoneEqualTo(Long.parseLong(TUserphone));
        criteria.andUseremailEqualTo(Tuseremail);
        criteria.andUserpasswordEqualTo(Tuserpassword);
        List<SesUser> list = sesUserDao.selectByExample(sesUserExample);
        /**
         * 添加医院信息
         */
        SesHospitalinfo sesHospitalinfo=new SesHospitalinfo();
       SesHospitalinfo sesHospitalinfo1= sesHospitalinfoDao.selectByPrimaryKey(Long.parseLong(TYiYuan));
        sesHospitalinfo.setHospitalinfoname(sesHospitalinfo1.getHospitalinfoname());
        sesHospitalinfo.setUserid(list.get(0).getUserid());
        sesHospitalinfoDao.insertSelective(sesHospitalinfo);
        SesHospitalinfoExample sesHospitalinfoExample=new SesHospitalinfoExample();
        SesHospitalinfoExample.Criteria criteria1 = sesHospitalinfoExample.createCriteria();
        criteria1.andHospitalinfonameEqualTo(TYiYuan);
        criteria1.andUseridEqualTo(list.get(0).getUserid());
        List<SesHospitalinfo> sesHospitalinfos = sesHospitalinfoDao.selectByExample(sesHospitalinfoExample);

        /**
         * 添加科室
         */
       SesHospitalinfoDepartment sesHospitalinfoDepartment=new SesHospitalinfoDepartment();
       SesHospitalinfoDepartment sesHospitalinfoDepartment1= sesHospitalinfoDepartmentDao.selectByPrimaryKey(Long.parseLong(TKeShi));
       sesHospitalinfoDepartment.setDepartmentname(sesHospitalinfoDepartment1.getDepartmentname());
       sesHospitalinfoDepartment.setUserid(list.get(0).getUserid());
       sesHospitalinfoDepartment.setDepartmentHospitalinfoid(sesHospitalinfos.get(0).getHospitalinfoid());
       sesHospitalinfoDepartmentDao.insertSelective(sesHospitalinfoDepartment);
    }

    /**
     * 编辑维修人员（数据回显）
     * @param userId
     */
    @Override
    public Map<String,Object> BianJiWeiXiu(Long userId) {
        SesUser sesUser = sesUserDao.selectByPrimaryKey(userId);
        SesHospitalinfoExample sesHospitalinfoExample=new SesHospitalinfoExample();
        SesHospitalinfoExample.Criteria criteria = sesHospitalinfoExample.createCriteria();
        criteria.andUseridEqualTo(userId);
        List<SesHospitalinfo> sesHospitalinfos = sesHospitalinfoDao.selectByExample(sesHospitalinfoExample);
        SesHospitalinfoDepartmentExample sesHospitalinfoDepartmentExample=new SesHospitalinfoDepartmentExample();
        SesHospitalinfoDepartmentExample.Criteria criteria1 = sesHospitalinfoDepartmentExample.createCriteria();
        criteria1.andUseridEqualTo(userId);
        List<SesHospitalinfoDepartment> sesHospitalinfoDepartments = sesHospitalinfoDepartmentDao.selectByExample(sesHospitalinfoDepartmentExample);

        Map<String,Object> map=new HashMap<>();
        map.put("sesHospitalinfos",sesHospitalinfos);
        map.put("sesHospitalinfoDepartments",sesHospitalinfoDepartments);
        map.put("sesUser",sesUser);
        return map;
    }

    /**
     * 编辑维修人员（保存数据）
     * @param username
     * @param userphone
     * @param bYiYuan
     * @param bKeShi
     * @param useremail
     * @param userpassword
     * @param userid
     */
    @Override
    public void BianJiWeiXiuBaoCun(String username, Long userphone, String bYiYuan, String bKeShi, String useremail, String userpassword, Long userid) {
        /**
         * 修改维修人员信息
         */
        SesUser sesUser=new SesUser();
        sesUser.setUsername(username);
        sesUser.setUserphone(userphone);
        sesUser.setUseremail(useremail);
        sesUser.setUserid(userid);
        sesUser.setUserpassword(userpassword);
        sesUserDao.updateByPrimaryKeySelective(sesUser);
        SesUser sesUser1 = sesUserDao.selectByPrimaryKey(userid);
//        /**
//         * 修改医院数据
//         */
//        SesHospitalinfo sesHospitalinfo=new SesHospitalinfo();
//        sesHospitalinfo.setHospitalinfoname(bYiYuan);
//        sesHospitalinfo.setHospitalinfoid(sesUser1.getSesHospitalinfo().getHospitalinfoid());
//        sesHospitalinfoDao.updateByPrimaryKeySelective(sesHospitalinfo);
//        /**
//         * 修改科室数据
//         */
//        SesHospitalinfoDepartment sesHospitalinfoDepartment=new SesHospitalinfoDepartment();
//        sesHospitalinfoDepartment.setDepartmentname(bKeShi);
//        sesHospitalinfoDepartment.setDepartmentid(sesUser1.getSesHospitalinfoDepartment().getDepartmentid());
//        sesHospitalinfoDepartmentDao.updateByPrimaryKeySelective(sesHospitalinfoDepartment);
    }

    /**
     * 得到维修人员详情
     * @param userid
     * @return
     */
    @Override
    public Map<String,Object> getOneUserWeiXiu(Long userid) {
        SesUser sesUser = sesUserDao.selectByPrimaryKey(userid);
        SesUserExample sesUserExample=new SesUserExample();
        SesUserExample.Criteria criteria = sesUserExample.createCriteria();
        criteria.andUseridEqualTo(userid);
        List<SesUser> list = sesUserDao.selectByExample(sesUserExample);
          List<Long> listUserID=new ArrayList<>();
        for (SesUser l:list) {
            listUserID.add(l.getUserid());
        }
        //查询医院信息
        SesHospitalinfoExample sesHospitalinfoExample=new SesHospitalinfoExample();
        SesHospitalinfoExample.Criteria criteria1 = sesHospitalinfoExample.createCriteria();
        criteria1.andUseridIn(listUserID);
        List<SesHospitalinfo> sesHospitalinfos = sesHospitalinfoDao.selectByExample(sesHospitalinfoExample);

     //查询科室信息
        SesHospitalinfoDepartmentExample sesHospitalinfoDepartmentExample=new SesHospitalinfoDepartmentExample();
        SesHospitalinfoDepartmentExample.Criteria criteria2 = sesHospitalinfoDepartmentExample.createCriteria();
        SesHospitalinfoDepartmentExample.Criteria criteria3 = criteria2.andUseridIn(listUserID);
        List<SesHospitalinfoDepartment> sesHospitalinfoDepartments = sesHospitalinfoDepartmentDao.selectByExample(sesHospitalinfoDepartmentExample);
    //查询状态床位编号
        SesBedMessageExample sesBedMessageExample=new SesBedMessageExample();
        SesBedMessageExample.Criteria criteria4 = sesBedMessageExample.createCriteria();
        criteria4.andUsermaintenanceidIn(listUserID);
        List<SesBedMessage> sesBedMessages = sesBedMessageDao.selectByExample(sesBedMessageExample);
        //查询维修状态，时间
        SesUserReportExample sesUserReportExample=new SesUserReportExample();
        SesUserReportExample.Criteria criteria5 = sesUserReportExample.createCriteria();
        criteria5.andUseridIn(listUserID);
        List<SesUserReport> sesUserReports = sesUserReportDao.selectByExample(sesUserReportExample);
        //查询损坏的信息
        SesUserFeedbackExample sesUserFeedbackExample=new SesUserFeedbackExample();
        SesUserFeedbackExample.Criteria criteria6 = sesUserFeedbackExample.createCriteria();
        criteria6.andUserUseridIn(listUserID);
        List<SesUserFeedback> sesUserFeedbacks = sesUserFeedbackDao.selectByExample(sesUserFeedbackExample);
        Map<String,Object> map=new HashMap<>();
        map.put("yiyuanname",sesHospitalinfos);
        map.put("keshiname",sesHospitalinfoDepartments);
        map.put("chuangwei",sesBedMessages);
        map.put("weixiu",sesUserReports);
        map.put("sunhuai",sesUserFeedbacks);
        map.put("user",sesUser);
        map.put("sesUser",list);
        return map;
    }

    /**
     * 得到一个人员和分销商信息
     * @param userId
     * @param sid
     * @return
     */
    @Override
    public Map<String, Object> getOneUseGuDong(Long userId, Long sid) {
        Map<String, Object> map=new HashMap<>();
        SesUser sesUser = sesUserDao.selectByPrimaryKey(userId);
        SesUserSharing sesUserSharing = sesUserSharingDao.selectByPrimaryKey(sid);
        //查询医院



        try {
            SesHospitalinfoExample     sesHospitalinfoExample = new SesHospitalinfoExample();
            SesHospitalinfoExample.Criteria criteria = sesHospitalinfoExample.createCriteria();
            criteria.andUsershareholderidEqualTo(userId);
            List<SesHospitalinfo> sesHospitalinfos = sesHospitalinfoDao.selectByExample(sesHospitalinfoExample);
            List<Long> userid =new ArrayList<>();
            for (SesHospitalinfo s:sesHospitalinfos) {
                userid.add(s.getUserid());
            }
            //得到所有医院对接人名字

            SesUserExample sesUserExample=new SesUserExample();
            SesUserExample.Criteria criteria1 = sesUserExample.createCriteria();
            criteria1.andUseridIn(userid);
            List<SesUser> list = sesUserDao.selectByExample(sesUserExample);



            //得到所有的科室

            SesHospitalinfoDepartmentExample sesHospitalinfoDepartmentExample=new SesHospitalinfoDepartmentExample();
            SesHospitalinfoDepartmentExample.Criteria criteria2 = sesHospitalinfoDepartmentExample.createCriteria();
            criteria2.andUsershareholderidEqualTo(userId);
            List<SesHospitalinfoDepartment> sesHospitalinfoDepartments = sesHospitalinfoDepartmentDao.selectByExample(sesHospitalinfoDepartmentExample);
            List<Long> userID=new ArrayList<>();


            for (SesHospitalinfoDepartment se:sesHospitalinfoDepartments) {
                userID.add(se.getUserid());
            }
            System.out.println(userID+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            //得到所有科室对接人
            SesUserExample sesUserExample1=new SesUserExample();
            SesUserExample.Criteria criteria3 = sesUserExample1.createCriteria();
            criteria3.andUseridIn(userID);
            List<SesUser> list1 = sesUserDao.selectByExample(sesUserExample1);
            map.put("yiyuan",sesHospitalinfos);
            map.put("keshi",sesHospitalinfoDepartments);
            map.put("yiname",list);
            map.put("kename",list1);
        } catch (Exception e) {
            map.put("yiyuan",null);
            map.put("keshi",null);
            map.put("yiname",null);
            map.put("kename",null);
        }finally {
            map.put("user",sesUser);
            map.put("sh",sesUserSharing);
            return map;
        }

    }

    @Override
    public Map<String, Object> yiyuankeshiliebiao() {
        List<SesHospitalinfo> sesHospitalinfos = sesHospitalinfoDao.selectByExample(null);
        List<SesHospitalinfoDepartment> sesHospitalinfoDepartments = sesHospitalinfoDepartmentDao.selectByExample(null);
        Map<String,Object> map=new HashMap<>();
        map.put("hos",sesHospitalinfos);
        map.put("hosd",sesHospitalinfoDepartments);
        return map;
    }


    /**
     * 获取全部用户
     * @return map集合
     */
    @Override
    public PageInfo<SesUser> info(UserSearchPojo userSearchPojo,Integer pageNum,Integer pageSize) {
        SesUserExample exampleUser = new SesUserExample();
        SesUserExample.Criteria criteriaUser = exampleUser.createCriteria();
        //获取的只能是用户，用户类型 1 and 9
        criteriaUser.andUsertypeIn(Arrays.asList(1,9));
        //判断是电话查询还是其他条件分开查询
        if(userSearchPojo.getFlag()!=null){
            if(userSearchPojo.getFlag()){
                if (userSearchPojo.getUserphone() != null) {//电话
                    criteriaUser.andUserphoneEqualTo(userSearchPojo.getUserphone());
                }
            }else{
                if (userSearchPojo.getUsertype() != null) {//类型
                    criteriaUser.andUsertypeEqualTo(userSearchPojo.getUsertype());
                }
                if (userSearchPojo.getUsersex() != null) {//性别
                    criteriaUser.andUsersexEqualTo(userSearchPojo.getUsersex());
                }

                //开始和结束同时存在
                if((userSearchPojo.getTimeBegin() != null && !userSearchPojo.getTimeBegin().equals(""))&&(userSearchPojo.getTimeOver() != null && !userSearchPojo.getTimeOver().equals(""))){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        Date dateBegin = sdf.parse(userSearchPojo.getTimeBegin());
                        Date dateOver = sdf.parse(userSearchPojo.getTimeOver());
                        criteriaUser.andUserregistrationtimeBetween(new Timestamp(dateBegin.getTime()),new Timestamp(dateOver.getTime()));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }else{
                    //单个存在
                    if (userSearchPojo.getTimeBegin() != null && !userSearchPojo.getTimeBegin().equals("")) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            Date date = sdf.parse(userSearchPojo.getTimeBegin());
                            criteriaUser.andUserregistrationtimeGreaterThanOrEqualTo(new Timestamp(date.getTime()));//开始时间
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    if (userSearchPojo.getTimeOver() != null && !userSearchPojo.getTimeOver().equals("")) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            Date date = sdf.parse(userSearchPojo.getTimeOver());
                            criteriaUser.andUserregistrationtimeLessThanOrEqualTo(new Timestamp(date.getTime()));//结束时间
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }



            }
        }
        //总用户数
        List<SesUser> count = GetUserDetails.getUserDetails(sesUserDao.selectByExample(exampleUser), sesUserOrderDao, sesBedMessageDao, sesHospitalinfoDao, sesHospitalinfoDepartmentDao);

        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<SesUser> list = sesUserDao.selectByExample(exampleUser);
        List<SesUser> result = GetUserDetails.getUserDetails(list, sesUserOrderDao, sesBedMessageDao, sesHospitalinfoDao, sesHospitalinfoDepartmentDao);
        result.get(0).setSize(count.size());
        PageInfo<SesUser> info = new PageInfo<>(result);
        return info;
    }

    /**
     * 通过 用户 Id 获取所有订单详情
     * @param userid
     * @return
     */
    @Override
    public List<SesUserOrder> orderInfo(Long userid) {
        SesUserOrderExample example = new SesUserOrderExample();
        SesUserOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserUseridEqualTo(userid);
        List<SesUserOrder> list = sesUserOrderDao.selectByExample(example);
        //查询当前订单所属的医院与科室
        for (SesUserOrder so:list) {
            //获得当前订单所属陪护床Id
            Long bedMessageid = so.getBedMessageid();
            //获得陪护床对象
            SesBedMessage sesBedMessage = sesBedMessageDao.selectByPrimaryKey(bedMessageid);
            //获得医院Id
            Long hospitalinfoid = sesBedMessage.getHospitalinfoid();
            SesHospitalinfo sesHospitalinfo = sesHospitalinfoDao.selectByPrimaryKey(hospitalinfoid);
            so.setHospital(sesHospitalinfo.getHospitalinfoname());//获得医院名字
            //获得科室Id
            Long departmentid = sesBedMessage.getHospitalinfoDepartmentid();
            SesHospitalinfoDepartment departmentInfo = sesHospitalinfoDepartmentDao.selectByPrimaryKey(departmentid);
            so.setDepartment(departmentInfo.getDepartmentname());//获得科室名
        }
        return list;
    }

    /**
     * 删除用户
     * @param userid
     */
    @Override
    public void deleteUser(Long userid) {
        sesUserDao.deleteByPrimaryKey(userid);
    }

    /**
     * 获取单个用户详情
     * @param userid
     */
    @Override
    public SesUser getUser(Long userid) {
        SesUser sesUser = sesUserDao.selectByPrimaryKey(userid);
        List<SesUser> list = new ArrayList<>();
        list.add(sesUser);
        List<SesUser> temp = GetUserDetails.getUserDetails(list, sesUserOrderDao, sesBedMessageDao, sesHospitalinfoDao, sesHospitalinfoDepartmentDao);
        return temp.get(0);
    }

    @Override
    public void update_user(SesUser sesUser) {
        sesUserDao.updateByPrimaryKeySelective(sesUser);
    }


}
